Object.defineProperty(exports, "__esModule", {
    value: true
});

exports.Circle = exports.MapPoints = exports.Polyline = exports.Marker = exports.PolylineList = exports.MarkerList = undefined;

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function(obj) {
    return typeof obj;
} : function(obj) {
    return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};

var _extends = Object.assign || function(target) {
    for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i];
        for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
                target[key] = source[key];
            }
        }
    }
    return target;
};

var _createClass = function() {
    function defineProperties(target, props) {
        for (var i = 0; i < props.length; i++) {
            var descriptor = props[i];
            descriptor.enumerable = descriptor.enumerable || false;
            descriptor.configurable = true;
            if ("value" in descriptor) descriptor.writable = true;
            Object.defineProperty(target, descriptor.key, descriptor);
        }
    }
    return function(Constructor, protoProps, staticProps) {
        if (protoProps) defineProperties(Constructor.prototype, protoProps);
        if (staticProps) defineProperties(Constructor, staticProps);
        return Constructor;
    };
}();

var _uplus = require("./uplus.js");

var _util = require("./util_2.js");

var _util2 = _interopRequireDefault(_util);

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}

function formatLabelContent(txt) {
    var len = _util2.default.getByteLen(txt);
    if (len > 20) {
        len = 20;
        for (var i = 0; i < txt.length; i++) {
            var a = txt.charAt(i);
            var after = txt.charAt(i + 1);
            if (a.match(/[^\x00-\xff]/gi) != null) {
                len -= 2;
            } else {
                len -= 1;
            }
            if (len <= 0) {
                //console.log(txt.substring())
                var sliceIndex = i + 1;
                if (/\(/i.test(a)) {
                    sliceIndex = sliceIndex - 1;
                }
                if (/\)/i.test(after)) {
                    sliceIndex = sliceIndex + 1;
                }
                var firstTxt = txt.slice(0, sliceIndex);
                var lastTxt = txt.slice(sliceIndex);
                txt = firstTxt + "\n" + lastTxt;
                break;
            }
        }
    }
    return txt;
}

var MarkerList = function() {
    function MarkerList() {
        _classCallCheck(this, MarkerList);
        this.markers = [];
    }
    _createClass(MarkerList, [ {
        key: "_findMarker",
        value: function _findMarker(marker) {
            var markerIndex = this.markers.findIndex(function(item) {
                return item.type === marker.type;
            });
            return markerIndex;
        }
    }, {
        key: "getMarkers",
        value: function getMarkers() {
            return this.markers;
        }
    }, {
        key: "getLastId",
        value: function getLastId() {
            var orderMarkers = this.markers.map(function(item) {
                return item.id;
            }).sort(function(a, b) {
                return a - b;
            });
            var nextId = 1;
            for (var i = 0, len = orderMarkers.length; i < len; i++) {
                if (orderMarkers[i] - nextId >= 1) {
                    break;
                } else {
                    nextId = orderMarkers[i] + 1;
                }
            }
            return nextId;
        }
    }, {
        key: "addMarkerList",
        value: function addMarkerList(markerList, isReplace) {
            var _this = this;
            if (isReplace) {
                this.markers = this.markers.filter(function(item) {
                    return markerList.getMarkers().every(function(i) {
                        return i.type !== item.type;
                    });
                });
            }
            markerList.getMarkers().forEach(function(item) {
                item.id = _this.getLastId();
                _this.markers.push(item);
            });
            return this;
        }
    }, {
        key: "addMarker",
        value: function addMarker(marker, isReplace, markerId) {
            marker = _extends({}, marker);
            if (isReplace) {
                var markerIndex = this._findMarker(marker);
                markerIndex >= 0 && this.markers.splice(markerIndex, 1);
            }
            marker.id = typeof markerId == "number" ? markerId : this.getLastId();
            this.markers.push(marker);
            return this;
        }
    }, {
        key: "removeMarkers",
        value: function removeMarkers(markerType) {
            this.markers = this.markers.filter(function(item) {
                return item.type !== markerType;
            });
            return this;
        }
    }, {
        key: "clear",
        value: function clear() {
            this.markers = [];
            return this;
        }
    } ]);
    return MarkerList;
}();

var Marker = function() {
    function Marker(point, markerType, payload) {
        _classCallCheck(this, Marker);
        if (point instanceof _uplus.Address) {
            this.setPoint(point.point);
        } else {
            this.setPoint(point);
        }
        if (typeof Marker.MarkerType[markerType] === "function") {
            Object.assign(this, Marker.MarkerType[markerType](payload));
        } else {
            Object.assign(this, Marker.MarkerType[markerType]);
        }
    }
    _createClass(Marker, [ {
        key: "setPoint",
        value: function setPoint(point) {
            this.longitude = point.lng;
            this.latitude = point.lat;
        }
    }, {
        key: "setProp",
        value: function setProp(propName, propValue) {
            this[propName] = propValue;
            return this;
        }
    }, {
        key: "setLabel",
        value: function setLabel(labelObj) {
            var defaultLabel = {
                content: "label",
                color: "#70bc14",
                fontSize: 12,
                anchorX: 5,
                anchorY: -8,
                padding: 1,
                textAlign: "center"
            };
            this.label = Object.assign(defaultLabel, this.label, labelObj);
            this.label.content = formatLabelContent(this.label.content);
            if (labelObj.isLeft) {
                try {
                    var len = _util2.default.getByteLen(this.label.content.split("\n")[0]);
                    var anchorX = Math.round(len * 6.5);
                    console.log(len, anchorX);
                    this.label.anchorX = -anchorX;
                } catch (e) {
                    console.log(e);
                }
            }
            return this;
        }
    }, {
        key: "setCallout",
        value: function setCallout(calloutObj) {
            var defaultCallout = {
                content: "",
                borderRadius: 10,
                padding: 10,
                display: "ALWAYS",
                textAlign: "center",
                fontSize: 12
            };
            this.callout = Object.assign(defaultCallout, this.callout, calloutObj);
            return this;
        }
    } ]);
    return Marker;
}();

var Polyline = function() {
    function Polyline(points, polylineType) {
        _classCallCheck(this, Polyline);
        this.setPoints(points);
        if (typeof polylineType === "string") {
            Object.assign(this, Polyline.PolylineType[polylineType]);
        } else if ((typeof polylineType === "undefined" ? "undefined" : _typeof(polylineType)) === "object") {
            Object.assign(this, polylineType);
        }
    }
    _createClass(Polyline, [ {
        key: "setPoints",
        value: function setPoints(points) {
            if (!Array.isArray(points)) {
                return;
            }
            this.points = points.map(function(item) {
                return {
                    longitude: item.lng || item.longitude,
                    latitude: item.lat || item.latitude
                };
            });
        }
    } ]);
    return Polyline;
}();

var PolylineList = function() {
    function PolylineList() {
        _classCallCheck(this, PolylineList);
        this.polylines = [];
    }
    _createClass(PolylineList, [ {
        key: "_findPolyline",
        value: function _findPolyline(polyline) {
            var theIndex = this.polylines.findIndex(function(item) {
                return item.type === polyline.type;
            });
            return theIndex;
        }
    }, {
        key: "getPolylines",
        value: function getPolylines() {
            return this.polylines;
        }
    }, {
        key: "addPolylineList",
        value: function addPolylineList(polylineList, isReplace) {
            var currentIndex = this.polylines.length;
            polylineList = polylineList.getPolylines().map(function(item, index) {
                item.id = _util2.default.createNonceStr();
                return item;
            });
            if (isReplace) {
                this.polylineList = this.polylineList.filter(function(item) {
                    return polylineList.every(function(i) {
                        return i.type !== item.type;
                    });
                });
            }
            this.polylines.concat(polylineList);
            return this;
        }
    }, {
        key: "addPolyline",
        value: function addPolyline(polyline, isReplace) {
            polyline = _extends({}, polyline, {
                id: _util2.default.createNonceStr()
            });
            if (isReplace) {
                var theIndex = this._findPolyline(polyline);
                theIndex >= 0 && this.polylines.splice(theIndex, 1);
            }
            this.polylines.push(polyline);
            return this;
        }
    }, {
        key: "removePolylines",
        value: function removePolylines(polylineType) {
            this.polylines = this.polylines.filter(function(item) {
                return item.type !== polylineType;
            });
            return this;
        }
    }, {
        key: "clear",
        value: function clear() {
            this.polylines = [];
            return this;
        }
    } ]);
    return PolylineList;
}();

var MapPoints = function MapPoints(points) {
    _classCallCheck(this, MapPoints);
    this.points = points.filter(function(item) {
        return !!item;
    }).map(function(item) {
        return {
            longitude: item.lng || item.longitude,
            latitude: item.lat || item.latitude
        };
    });
};

var Circle = function() {
    function Circle(point, circleType) {
        _classCallCheck(this, Circle);
        this.setPoint(point);
        if ((typeof circleType === "undefined" ? "undefined" : _typeof(circleType)) === "object") {
            Object.assign(this, circleType);
        }
    }
    _createClass(Circle, [ {
        key: "setPoint",
        value: function setPoint(point) {
            this.longitude = point.lng;
            this.latitude = point.lat;
        }
    }, {
        key: "setProp",
        value: function setProp(propName, propValue) {
            this[propName] = propValue;
        }
    } ]);
    return Circle;
}();

exports.MarkerList = MarkerList;

exports.PolylineList = PolylineList;

exports.Marker = Marker;

exports.Polyline = Polyline;

exports.MapPoints = MapPoints;

exports.Circle = Circle;